/**
 * 商品详情页添加到购物车
 */
function addCart(){
    var entityId=$("input[name='entityId']").val();
    var quantity=$("input[name='quantity']").val();
    //添加到购物车
    addCartByParam(entityId,quantity);
}

/**
 * 商品列表页添加到购物车
 * @param entityId
 * @param quantity
 */
function addCartByParam(entityId,quantity){
    //添加到购物车
    $.ajax({
        url: contextPath + "/CartServlet",
        method: "post",
        data: {
            action: "add",
            entityId: entityId,
            quantity: quantity
        },
        success: function (jsonStr) {
        	 var result = eval("(" + jsonStr + ")");
             //状态判断
             if (result.status == 1) {
            	 showMessage("操作成功");
            	 refreshCart();
             }else{
            	 showMessage(result.message);
             }
        }
    } )
}


/**
 * 刷新购物车 searchBar DIV
 */
function refreshCart(){
    $.ajax({
        url: contextPath + "/CartServlet",
        method: "post",
        data: {
            action: "refreshCart"
        },
        success: function (jsonStr) {
            $("#searchBar").html(jsonStr);
        }
    })
}


function settlement1(){
    $.ajax({
        url: contextPath + "/CartServlet",
        method: "post",
        data: {
            action: "toSettlment1"
        },
        success: function (jsonStr) {
        	refreshCart();
            $("#settlement").html(jsonStr);
        }
    });
}


/**
 * 减去 数量减
 * @param obj
 * @param entityId
 */
function subQuantity(obj,entityId){
    var quantity=getPCount(jq(obj))-1;
    if(quantity==0) quantity=1;
    modifyCart(entityId,quantity,jq(obj));
}


function addQuantity(obj,entityId,stock){
	var quantity = Number(getPCount(obj)+1);
	if (stock < quantity) {
		showMessage("商品数量不足");
		return;
	}
	//进行商品数量的修改
	modifyCart(entityId,quantity,obj);
}

/**
 * 	修改购物车的商品数量
 * @param entityId
 * @param quantity
 * @param obj
 * @returns
 */
function modifyCart(entityId,quantity,obj){
	$.ajax({
        url: contextPath + "/CartServlet",
        method: "post",
        data: {
            action: "modifyCart",
            entityId:entityId,
            quantity:quantity
        },
        success: function (jsonStr) {
        	var result = eval("(" + jsonStr + ")");
            //状态判断
            if (result.status == 1) {
                obj.parent().find(".car_ipt").val(quantity);
                settlement1();
            }else{
           	 	showMessage(result.message);
            }
        }
    });
}


function removeCart(entityId) {
	$.ajax({
        url: contextPath + "/CartServlet",
        method: "post",
        data: {
            action: "modifyCart",
            entityId:entityId,
            quantity:0
        },
        dataType:"JSON",
        success:function(jsonStr){
        	if (jsonStr.status == 1) {
        		settlement1();
			} else {
				showMessage(jsonStr.message);
			}
        }
    });
}


function addFavorite(entityId){
	$.ajax({
        url: contextPath + "/FavoriteServlet",
        method: "post",
        data: {
            action: "addFavorite",
            entityId:entityId
        },
        dataType:"JSON",
        success:function(jsonStr){
        	favoriteList();
        }
    });
}


function favoriteList(){
	$.ajax({
        url: contextPath + "/FavoriteServlet",
        method: "post",
        data: {
            action: "favoriteList"
        },
        success:function(jsonStr){
        	$("#favoriteList").html(jsonStr);
        }
    });
}